/*
 *
 *
 */

#include "InitialState.h"

#ifndef _INITIALSTATE_CPP_
#define _INITIALSTATE_CPP_

#define TOTAL_TIME 60000
#define SCORE_WINNER 100

/*
 *
 */
void InitialState::load(void)
{
    background = new Sprite("../Images/background_2.jpg",0,0);
    music = new Audio("../Sounds/music_01.mp3",1);
    inputManager = Singleton<InputManager>::getInstance();
    #ifdef _TRACKING_
	    trackManager = Singleton<TrackManager>::getInstance();
	    body = trackManager->getBody();
	#endif
    smileManager = new SmileManager("../Stages/stage01.txt");
    leftHand = new Sprite("../Images/leftHand.png",0,0);
    rightHand = new Sprite("../Images/rightHand.png",0,0);
    messageScore = new Text("Score: 0","../Fonts/liberation_sans.ttf",500.0,440.0,TTF_STYLE_BOLD,255,0,0,22);
    hudScoreBackground = new Sprite("../Images/hud_background.png",490,440);
    messageTime = new Text("Time: 0","../Fonts/liberation_sans.ttf",20.0,440.0,TTF_STYLE_BOLD,0,0,255,22);
    hudTimeBackground = new Sprite("../Images/hud_background.png",10,440);
    messageWinLose = new Text("????","../Fonts/liberation_sans.ttf",40.0,220.0,TTF_STYLE_BOLD,255,0,0,42);
    music->play(0);
    score = 0;
    clock.start();
}

/*
 *
 */
int InitialState::update(int tDelta)
{
	stringstream bufferScore, bufferTime;
    inputManager->poolEvents(); /*Este método deve ser chamado no início do update de cada estado que utiliza a classe InputManager*/

    if(inputManager->isEventQuit())
        return -1;
    if(inputManager->isKeyDown(SDLK_ESCAPE))
        return 3; 
    
    /*atualiza as posições das mãos*/
    #ifdef _TRACKING_
	    leftHand->updatePos(body->left_centroid.x,body->left_centroid.y);
	    rightHand->updatePos(body->right_centroid.x,body->right_centroid.y);
	#endif
    
    /*faz o update dos smiles na tela*/
    score += smileManager->update(tDelta,leftHand,rightHand);
    
    /*faz um udate da mensagem de Score, para o numero correto*/
    bufferScore << "Score: " << score;
    messageScore->changeText(bufferScore.str());
        
        
    if(TOTAL_TIME >= clock.getTime())
	{
	    /*faz um update da mensagem de Tempo para o numero correto*/
	    bufferTime << "Time: " << (TOTAL_TIME - clock.getTime())/1000;
	    messageTime->changeText(bufferTime.str());
	    /*altera a cor da mensagem de tempo se o tempo esta acabando*/
	    if((TOTAL_TIME - clock.getTime())/1000 <= 10)
	    	messageTime->changeColor(255,0,0);
    }
    else
    {
		stringstream winLoseStream;
		if(score >= SCORE_WINNER)
		{
			winLoseStream << "Great, your Score: " << score; 
		}
		else
		{
			winLoseStream << "Try again, your Score: " << score; 
		}
		messageWinLose->changeText(winLoseStream.str());	
	}
    
    #ifdef _TRACKING_
	    /*altera o tipo de segmentação utilizado*/
	    if(inputManager->isKeyDown(SDLK_1))
	        trackManager->setSegmantation(0);
	    if(inputManager->isKeyDown(SDLK_2))
	        trackManager->setSegmantation(1);
	    if(inputManager->isKeyDown(SDLK_3))
	        trackManager->setSegmantation(2);
	    if(inputManager->isKeyDown(SDLK_4))
	        trackManager->setSegmantation(3);
	#endif
    return 0;
}

/*
 *
 */
void InitialState::draw(SDL_Surface *screen)
{
    background->render(screen);
    smileManager->render(screen);
    
    /*desenha as mãos*/
    leftHand->render(screen);
    rightHand->render(screen);
    
    /*desenha o Score*/
    hudScoreBackground->render(screen);
    messageScore->render(screen);
    
    /*desenha a mensagem de tempo*/
    hudTimeBackground->render(screen);
    messageTime->render(screen);
    
    if(TOTAL_TIME < clock.getTime())
    	messageWinLose->render(screen);
}

/*
 *
 */
void InitialState::unload(void)
{
	if(hudTimeBackground != NULL)
    {
        delete hudTimeBackground;
        hudTimeBackground = NULL;
    }
	if(messageWinLose != NULL)
    {
        delete messageWinLose;
        messageWinLose  = NULL;
    }
	if(messageTime != NULL)
    {
        delete messageTime;
        messageTime  = NULL;
    }
	if(hudScoreBackground != NULL)
    {
        delete hudScoreBackground;
        hudScoreBackground = NULL;
    }
	if(messageScore != NULL)
    {
        delete messageScore;
        messageScore  = NULL;
    }
    if(rightHand != NULL)
    {
        delete rightHand;
        rightHand  = NULL;
    }
    if(leftHand != NULL)
    {
        delete leftHand;
        leftHand  = NULL;
    }
    if(smileManager != NULL)
    {
        delete smileManager ;
        smileManager  = NULL;
    }
    if(inputManager != NULL)
    {
        Singleton<InputManager>::releaseInstance();
    }
    #ifdef _TRACKING_
	    if(trackManager != NULL)
	    {
	        Singleton<TrackManager>::releaseInstance();
	    }
	#endif
    if(music != NULL)
    {
        delete music;
        music = NULL;
    }
    if(background != NULL)
    {
        delete background;
        background = NULL;
    }
}

#endif
